package com.gg.cart.service.impl;

import com.gg.cart.mapper.CartMapper;
import com.gg.cart.service.CartService;
import com.gg.entity.Cart;
import com.gg.entity.Fruit;
import com.gg.povos.CartFruitPovo;
import com.gg.helper.UUIDHelper;

import java.util.List;

public class CartServiceImpl implements CartService {
    /*
     * Service层 依赖  Dao层
     * */
    private CartMapper cartMapper = new CartMapper();

    /*查看购物车的具体实现*/
    @Override
    public List<CartFruitPovo> getCartsByUid(String uid) {
        return cartMapper.selectCartFruitPovosByUid(uid);
    }

    /*删除购物车  具体实现*/
    @Override
    public void removeCartByCid(String cid) {
        cartMapper.deleteCartByCid(cid);
    }


    @Override
    public boolean updateCartByCidForCcount(String cid, Integer count) {
        int res = cartMapper.updateCartByCidForCcount(cid, count);
        if (res == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean addToCart(Cart cart, Fruit fruit) {
        try {
            // 生成购物车条目ID
            cart.setCid(UUIDHelper.getUUID());
            // 调用mapper插入数据
            cartMapper.insertCart(cart, fruit);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}

